<?php

/**
 * file actions.
 *
 * @package    DocFlow
 * @subpackage file
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class fileActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->forward404('Index Unused yet');
  }

  public function executeUpload(sfWebRequest $request)
  {
    $document_id = $request->getParameter('id');
    $document = DocumentTable::getInstance()->find(array($document_id));
    $this->forward404Unless($document, sprintf('Object document does not exist (%s).', $document_id));

    $file = new File();
    $file->setDocumentId($document_id);
    $this->form = new FileForm($file);
    //$this->generateAndSaveNewDocVerToFiles( all uploaded files);    
  }

  public function executeDownload(sfWebRequest $request)
  {
      $fileId = $request->getParameter('id');
      $this->file = FileTable::getInstance()->find($fileId);
      $this->forward404Unless($this->file, sprintf('Object file does not exist (%s).', $fileId));
  }
  
  public function executeDoDownload(sfWebRequest $request)
  {
      $fileId = $request->getParameter('id');
      $this->file = FileTable::getInstance()->find($fileId);
      $this->forward404Unless($this->file, sprintf('Object file does not exist (%s).', $fileId));

      $modification = 0;
      if ($request->getPostParameter('check')!=null)
        $modification = 1;

      $userId = $this->getUser()->getId();
      FileTable::getInstance()->setLastLoaderQuery($fileId, $userId);
      FileDownloadHistoryTable::getInstance()->setFileDownloadHistory($fileId, $userId,$modification);
     
      $this->redirect($this->file->getFullName());

      
     // $this->getResponse()->setContentType("application/octet-stream");
     // $this->getResponse()->setHttpHeader("Content-Disposition", "attachment; filename=" . $file->getName());
     // $this->getResponse()->setHttpHeader("Content-Location", $file->getFullName());
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->forward404('Show Unused yet');
  }

  public function executeDocFiles(sfWebRequest $request)
  {
      $document_id = $request->getParameter('id');
      $this->document = DocumentTable::getInstance()->find(array($document_id));
      $this->forward404Unless($this->document, sprintf('Object document does not exist (%s).', $document_id));

      $this->files = FileTable::getInstance()->findAllExistingFilesOfDocument($document_id);
      $this->documentName = $this->document->getName();
    
      $this->getUser()->addDocumentToHistory($document_id);

      $this->show_file=true;
      $user_id = $this->getUser()->getId();
      $creator_id = DocumentVersionTable::findCreatorId($document_id);
      if ($creator_id != $user_id)
        $this->show_file=false;

  }

  public function executeFileVersions(sfWebRequest $request)
  {
      $document_id = $request->getParameter('id');
      $this->files = FileVersionTable::getInstance()->findAllExistingFilesOfDocument($document_id);
  }

  public function executeFileHistory(sfWebRequest $request)
  {
      $fileId = $request->getParameter('id');
      $file = FileTable::getInstance()->find($fileId);
      $this->forward404Unless($file, sprintf('Object file does not exist (%s).', $fileId));  
      
      $this->fileDownloadHistory = FileDownloadHistoryTable::getInstance()->findBy('fileId', $fileId);
      $this->forward404Unless($this->fileDownloadHistory);
      
      $document_id = $request->getParameter('document_id');
      $this->document = DocumentTable::getInstance()->find(array($document_id));
      $this->forward404Unless($this->document, sprintf('Object file does not exist (%s).', $document_id));

   
      $this->documentName = $this->document->getName();
      $this->fileNameForUsers = $file->getNameForUsers();
  }


  public function executeNew(sfWebRequest $request)
  {
    $document_id = $request->getParameter('id');
    $document = DocumentTable::getInstance()->find(array($document_id));
    $this->forward404Unless($document, sprintf('Object file does not exist (%s).', $document_id));

    $file = new File();
    $file->setDocumentId($document_id);
    $this->form = new FileForm($file);
  }
  
  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));
    $filePost = $request->getPostParameter('file');
    $this->forward404Unless($filePost, sprintf('Object file does not exist.'));

    $file = new File($filePost);

    $this->form = new FileForm($file);
    $this->processForm($request, $this->form);
    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $file_id = $request->getParameter('id');
    $file = FileTable::getInstance()->find(array($file_id));
    $this->forward404Unless($file, sprintf('Object file does not exist (%s).', $file_id));  

    $document_id = $request->getParameter('document_id');
    $document = DocumentTable::getInstance()->find(array($document_id));
    $this->forward404Unless($document, sprintf('Object document does not exist (%s).', $document_id));

    $this->form = new FileForm($file);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $file_id = $request->getParameter('id');
    $file = FileTable::getInstance()->find(array($file_id));
    $this->forward404Unless($file, sprintf('Object file does not exist (%s).', $file_id));

    $this->form = new FileForm($file);
    $this->processForm($request, $this->form);
    $this->setTemplate('edit');
  }
  
  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $file_id = $request->getParameter('id');
    $file = FileTable::getInstance()->findOneBy('id',$file_id);
    $file->setIsDeleted(1);
    $file->save();

    $this->redirect('@homepage');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $file = $form->save();
       
      $this->generateAndSaveNewDocVerToFiles(array($file));
    }
  }
  
  /*
   * Method generates and saves new DocumentVersion, DocumentVersionFiles to all files of document. 
   * Also it does redirect to @doc_files-Page
   */
  protected function generateAndSaveNewDocVerToFiles($changed_files)
  {
  	if (!empty($changed_files))
  	{
          $document_id = $changed_files[0]->getDocumentId();
          $document = DocumentTable::getInstance()->find(array($document_id));
          $this->forward404Unless($document, sprintf('Object document does not exist (%s).', $document_id));
      
  	  $all_files = FileTable::getInstance()->findAllFilesOfDocument($document_id);
  	  
  	  if (!empty($all_files))
  	  {
            $document->setAuthorId($this->getUser()->getId());

            //need to update version in any case(if one file was cause of two by order versions)
            $document->setUpdatedAt($changed_files[0]->getUpdatedAt());

            // now 'save' updates DocumentVersionFiles themself.
            $document->save();
     } 
  	}
  	
  	$this->redirect('@doc_files?id=' . $document_id);
  }
}
